package quq.missq.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import quq.missq.Constants;
import quq.missq.MLog;
import quq.missq.R;
import quq.missq.beans.Author;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.VolleyTool;

/**
 * Created by River on 8/11/15.
 */
public class MyContractorAdapter extends BaseAdapter{

    private Context mContext;
    private ArrayList<Author> authors;

    public MyContractorAdapter(Context context){
        this.mContext = context;
    }

    public void setData(ArrayList<Author> authors){
        this.authors = authors;
    }

    public ArrayList<Author> getData(){
        return this.authors;
    }

    @Override
    public int getCount() {
        return authors.size();
    }

    @Override
    public Author getItem(int position) {
        return authors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       final Author author = authors.get(position);
        Holder holder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.my_contractor_adapter,null);
            holder =  new Holder();
            holder.imageView = (ImageView)convertView.findViewById(R.id.avtar);
            holder.level = (TextView)convertView.findViewById(R.id.level);
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.gender = (ImageView)convertView.findViewById(R.id.gender);
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag();
        }
       // holder.imageView.setDefaultImageResId(R.mipmap.empty_photo);
        //holder.imageView.setImageUrl(Constants.IMAGE_HOST + author.getAvatar80(), VolleyTool.getImageLoader());
       // MLog.e(Constants.IMAGE_HOST + author.getAvatar());
        int i = author.getAvatar().indexOf('?');
        String url = author.getAvatar().substring(0,i);
        MLog.e(Constants.IMAGE_HOST + url);
        ImageLoadUtil.loadImage(holder.imageView, Constants.IMAGE_HOST + url, R.mipmap.empty_photo);
        holder.level.setText("Level"+author.getLevel());
        holder.name.setText(author.getNkname());
        if(author.getGender() == 1){
            holder.gender.setImageResource(R.mipmap.boy_icon);
        }else{
            holder.gender.setImageResource(R.mipmap.girl_icon);
        }
        return convertView;
    }

    private class Holder{
        ImageView imageView;
        TextView level;
        TextView name;
        ImageView gender;
    }

}
